package com.ego.order.listener;

import com.ego.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 〈〉
 *
 * @author coach tam
 * @email 327395128@qq.com
 * @create 2020/12/7
 * @since 1.0.0
 * 〈坚持灵活 灵活坚持〉
 */
@Slf4j
@Component
public class SeckillListener {

    @Resource
    private OrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "ego.seckill.queue", durable = "true"),
            exchange = @Exchange(value = "ego.seckill.exchange",
                    ignoreDeclarationExceptions = "true"),
            key = {"ego.seckill"}))
    public void sendSms(Map<String,Object> map) {

        try {
            String orderId = map.get("orderId").toString();
            Long skuId = (Long) map.get("skuId");
            Long price = (Long)map.get("price");
            Long userId = (Long)map.get("userId");

            //创建秒杀订单
            orderService.submitSeckillOrder(orderId, skuId, price,userId);

        } catch (Exception e) {
            log.error("创建秒杀订单失败",e);
        }

    }
}
